{% set template = haproxy_template.exchange %}
# Exchange Defaults
defaults
	# Default to HTTP mode
	mode http

	# Retry another server on connection failure
	option redispatch

	# Timeouts
	timeout connect            5s
	timeout http-keep-alive    1s
	timeout http-request       15s
	timeout queue              30s
	timeout tarpit             1m
	timeout client             300s
	timeout server             300s

	# Logging options
	option httplog
	option dontlognull
	log global

	# Set default balancing algorithm
{% if proxy.defaults is defined %}
	balance {{ proxy.defaults.balance|default(template.balance) }}
{% else %}
	balance {{ template.balance }}
{% endif %}

	# Default server check options
{% if proxy.defaults is defined %}
	default-server inter {{ proxy.defaults.interval|default(template.interval) }} rise {{ proxy.defaults.rise|default(template.rise) }} fall {{ proxy.defaults.fall|default(template.fall) }}
{% else %}
	default-server inter {{ template.interval }} rise {{ template.rise }} fall {{ template.fall }}
{% endif %}
 
# Exchange Front-End
frontend fe_{{ name }}
	# Listen on 80 and 443
{% for bind in proxy.frontend.bind %}
	bind {{ bind.ip }}:443 ssl crt {{ bind.crt }}
	bind {{ bind.ip }}:80
{% endfor %}

	# Redirect HTTP -> HTTPS
	redirect scheme https code 301 if !{ ssl_fc }

	# Add "X-Forwarded-For" header
	option forwardfor

	# ACLs to route traffic
{% for domain in proxy.frontend.autodiscover_domains %}
	acl sni_autod ssl_fc_sni -i autodiscover.{{ domain }}
{% endfor %}
	acl sni_{{ name }} ssl_fc_sni -i {{ proxy.frontend.hostname }}
	acl url_autod path_beg -i /Autodiscover/
	acl url_owa path_beg -i /owa/
	acl url_rpc path_beg -i /rpc/
	acl url_oab path_beg -i /OAB/
	acl url_ews path_beg -i /ews/
	acl url_ecp path_beg -i /ecp/
	acl url_mapi path_beg -i /mapi/
	acl url_eas path_beg -i /Microsoft-Server-ActiveSync/

	# Use specific back-end
	use_backend be_{{ name }}_autod if url_autod sni_autod
	use_backend be_{{ name }}_owa if url_owa sni_{{ name }}
	use_backend be_{{ name }}_rpc if url_rpc sni_{{ name }}
	use_backend be_{{ name }}_mapi if url_mapi sni_{{ name }}
	use_backend be_{{ name }}_oab if url_oab sni_{{ name }}
	use_backend be_{{ name }}_ews if url_ews sni_{{ name }}
	use_backend be_{{ name }}_ecp if url_ecp sni_{{ name }}
	use_backend be_{{ name }}_eas if url_eas sni_{{ name }}

	# Default backend if no other match
	use_backend be_{{ name }}_default

# Exchange Autodiscover Back-End
backend be_{{ name }}_autod
	# Health check options
	option httpchk GET /Autodiscover/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Enable compression
	compression algo gzip
	compression type text/html text/plain text/css text/xml application/xml application/x-javascript

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange OWA Back-End
backend be_{{ name }}_owa
	# Health check options
	option httpchk GET /owa/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Enable compression
	compression algo gzip
	compression type text/html text/plain text/css text/xml application/xml application/x-javascript

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange RPC Back-End
backend be_{{ name }}_rpc
	# Health check options
	option httpchk GET /rpc/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange MAPI Back-End
backend be_{{ name }}_mapi
	# Health check options
	option httpchk GET /mapi/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Enable compression
	compression algo gzip
	compression type text/html text/plain text/css text/xml application/xml application/x-javascript

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange EWS Back-End
backend be_{{ name }}_ews
	# Health check options
	option httpchk GET /ews/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange OAB Back-End
backend be_{{ name }}_oab
	# Health check options
	option httpchk GET /OAB/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Enable compression
	compression algo gzip
	compression type text/html text/plain text/css text/xml application/xml application/x-javascript

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange ECP Back-End
backend be_{{ name }}_ecp
	# Health check options
	option httpchk GET /ecp/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Enable compression
	compression algo gzip
	compression type text/html text/plain text/css text/xml application/xml application/x-javascript

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange ActiveSync Back-End
backend be_{{ name }}_eas
	# Health check options
	option httpchk GET /Microsoft-Server-ActiveSync/healthcheck.htm HTTP/1.1\r\nHost:\ {{ proxy.backend.hostname|default(proxy.frontend.hostname) }}\r\nConnection:\ Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}

# Exchange default Back-End
backend be_{{ name }}_default
	# Enable compression
	compression algo gzip
	compression type text/html text/plain text/css text/xml application/xml application/x-javascript

	# Exchange servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 ssl check ca-file {{ server.ca_file }}
{% endfor %}
